import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:lottie/lottie.dart';
import 'package:search_people/page/home/subs/person_detail/widgets/header.dart';
import 'package:search_people/utils/component/component.dart';
import 'package:search_people/utils/component/list/table_view.dart';
import 'package:search_people/utils/img_util.dart';
import 'package:search_people/value/topcolors.dart';
import 'package:share_plus/share_plus.dart';

import 'index.dart';
import 'widgets/widgets.dart';

class PersonDetailPage extends StatelessWidget {
  PersonDetailPage({Key? key}) : super(key: key);
  final controller = PersonDetailController();
  // 主视图
  Widget _buildView(PersonDetailController controller) {
    var state = controller.state;
    return TableView(
        controller: state.sc,
        header: PersonDetailHeader(
          controller: controller,
        ),
        data: state.isShowDynamic ? state.list : []);
  }

  @override
  Widget build(BuildContext context) {
    Get.create(
      () => controller,
      tag: controller.state.leaderId,
    );

    return GetBuilder<PersonDetailController>(
        init: controller,
        global: false,
        id: controller.state.leaderId,
        builder: (controller) {
          return Scaffold(
            appBar: detailAppBar(context,
                title: controller.state.baseMd?.name,
                actions: [
                  InkWell(
                    onTap: () {
                      if (!controller.state.isSubscribed) {
                        EasyLoading.show(
                          status: '订阅成功',
                          indicator:
                              LottieBuilder.asset('assets/json/subscribe.json'),
                        ).then(
                          (value) => Future.delayed(
                            const Duration(milliseconds: 1200),
                          ).then(
                            (value) => EasyLoading.dismiss(),
                          ),
                        );
                      } else {
                        EasyLoading.showToast('已取消订阅');
                      }

                      controller.state.isSubscribed =
                          !controller.state.isSubscribed;

                      controller.update();
                    },
                    child: Container(
                      width: 32.r,
                      padding: EdgeInsets.only(right: 5.r, left: 5.r),
                      child: Image.asset(
                        controller.state.isSubscribed
                            ? 'subscribe_y.png'.imgPathHome
                            : 'subscribe.png'.imgPathHome,
                      ),
                    ),
                  ),
                  InkWell(
                    onTap: () {
                      Share.share(
                          '${controller.state.baseMd?.name ?? ''} https://njkzy.szzfyjzx.icu/guanzheng/#/person_detail/leaderId/${controller.state.baseMd?.leaderId}',
                          subject: '快来看吧！');
                    },
                    child: Container(
                      width: 39.r,
                      padding: EdgeInsets.only(right: 12.r, left: 5.r),
                      child: Image.asset(
                        'alx.png'.imgPathHome,
                      ),
                    ),
                  ),
                ]),
            body: SafeArea(
              child: _buildView(controller),
            ),
          );
        });
  }
}
